package com.asm.DBloader;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ASM_FAvHedDS {
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;

	
	public ASM_FAvHedDS (Context context)
	{
		this.context = context;
		dbHelper = new ASM_DB(context);
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	public int Create(ASM_FAvHed det) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		ContentValues values = new ContentValues();
		values.put(ASM_DB.ASM_FAVHED_REFNO,det.getASM_FAVHED_REFNO());
		values.put(ASM_DB.ASM_FAVHED_TXNDATE,det.getASM_FAVHED_TXNDATE());
		values.put(ASM_DB.ASM_FAVHED_REMARKS,det.getASM_FAVHED_REMARKS());
		values.put(ASM_DB.ASM_FAVHED_ADDUSER,det.getASM_FAVHED_ADDUSER());
		values.put(ASM_DB.ASM_FAVHED_ADDDATE,det.getASM_FAVHED_ADDDATE());
		values.put(ASM_DB.ASM_FAVHED_ADDMACH,det.getASM_FAVHED_ADDMACH());
		values.put(ASM_DB.ASM_FAVHED_TERATORY,det.getASM_FAVHED_TERATORY());
		values.put(ASM_DB.ASM_FAVHED_ROOT,det.getASM_FAVHED_ROOT());
		values.put(ASM_DB.ASM_FAVHED_DEBTOR,det.getASM_FAVHED_DEBTOR());
		values.put(ASM_DB.ASM_FAVHED_BRAND,det.getASM_FAVHED_BRAND());
		
		values.put(ASM_DB.ASM_FAVHED_Longitude,det.getASM_FAVHED_Longitude());
		values.put(ASM_DB.ASM_FAVHED_Latitude,det.getASM_FAVHED_Latitude());
		values.put(ASM_DB.ASM_FAVHED_ISSYNCED,det.getASM_FAVHED_ISSYNCED());

		count = (int) dB.insert(ASM_DB.ASM_FAVHED, null, values);

		dB.close();
		return count;
		
	}
	
	
	public int Update(ASM_FAvHed det) 
	{
		
		int count =0;
		
		if(dB == null)
		{
			open();
		}else if(!dB.isOpen())
		{
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.ASM_FAVHED_REFNO,det.getASM_FAVHED_REFNO());
		values.put(ASM_DB.ASM_FAVHED_TXNDATE,det.getASM_FAVHED_TXNDATE());
		values.put(ASM_DB.ASM_FAVHED_REMARKS,det.getASM_FAVHED_REMARKS());
		values.put(ASM_DB.ASM_FAVHED_ADDUSER,det.getASM_FAVHED_ADDUSER());
		values.put(ASM_DB.ASM_FAVHED_ADDDATE,det.getASM_FAVHED_ADDDATE());
		values.put(ASM_DB.ASM_FAVHED_ADDMACH,det.getASM_FAVHED_ADDMACH());
		values.put(ASM_DB.ASM_FAVHED_TERATORY,det.getASM_FAVHED_TERATORY());
		values.put(ASM_DB.ASM_FAVHED_ROOT,det.getASM_FAVHED_ROOT());
		values.put(ASM_DB.ASM_FAVHED_DEBTOR,det.getASM_FAVHED_DEBTOR());
		values.put(ASM_DB.ASM_FAVHED_BRAND,det.getASM_FAVHED_BRAND());
	
		count = (int) dB.update(ASM_DB.ASM_FAVHED, values, "RefNo = '"+det.getASM_FAVHED_REFNO()+"'", null);

		dB.close();
		return count;
		
	}
	
	public ArrayList<ASM_FAvHed> getfAVHed(String refNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}

	
		String selectQuery = "select RefNo,TxnDate,Remarks,Teratory,Root,Debtor,Brand from fAvHed where RefNo = '"+refNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		ArrayList<ASM_FAvHed> list = new ArrayList<ASM_FAvHed>();
		while(cursor.moveToNext())
		{
		
			ASM_FAvHed AVHed=new ASM_FAvHed();
			
			AVHed.setASM_FAVHED_REFNO(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVHED_REFNO)));
			AVHed.setASM_FAVHED_TXNDATE(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVHED_TXNDATE)));
			AVHed.setASM_FAVHED_REMARKS(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVHED_REMARKS)));
			AVHed.setASM_FAVHED_TERATORY(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVHED_TERATORY)));
			AVHed.setASM_FAVHED_ROOT(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVHED_ROOT)));
			AVHed.setASM_FAVHED_DEBTOR(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVHED_DEBTOR)));
			AVHed.setASM_FAVHED_BRAND(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVHED_BRAND)));

			list.add(AVHed);
		
			
			
		}
		
		return list;
	}
	
	public ArrayList<String> getfAVHedfull() 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery = "select RefNo,TxnDate,Remarks,Teratory,Root,Debtor,Brand from fAvHed ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			//String RefNo = cursor.getString(0);
//[AVAA/0000001-:--:-Gjk-:-Gampaha -:- TCM004-:-SARATH MAWATHA -:- RT0034-:-SUPUN STORES -:- RE001930-:-APSARA -:- BAS001]
			
			
			Details.add(cursor.getString(0)
					+"~"+cursor.getString(1)
					+"~"+cursor.getString(2)
					+"~"+cursor.getString(3)
					+"~"+cursor.getString(4)
					+"~"+cursor.getString(5)
					+"~"+cursor.getString(6));
		
			
			
		}
		
		return Details;
	}
	public int Delete(String Refno)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FAVHED, "RefNo = '"+Refno+"'", null);

		dB.close();
		return count;
		
	}
}
